﻿using System;
using System.Windows.Forms;

namespace Operacional
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();                        
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtNome.Text))
            {
                Program.Entrada.lblStatus.Text = "Utilizador: " + txtNome.Text;
                frmLinhas Linhas = Application.OpenForms["frmLinhas"] as frmLinhas;

                Program.Entrada.LockTool.Image = Properties.Resources.UNLOCKED;

                Linhas = new frmLinhas();
                Linhas.MdiParent = Program.Entrada;
                Linhas.Show();

                FormClosing -= new FormClosingEventHandler(frmLogin_FormClosing);
                Close();
            }
            else
            {
                this.txtCodigo.Focus();
                MessageBox.Show("Sem utilizador definido!", "Erro");
            }
        }

        private void txtCodigo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) this.btnLogin_Click(sender, e);
        }


        private void txtCodigo_TextChanged(object sender, EventArgs e)
        {
            try
            { frmEntrada.dvUsers.RowFilter = "Codigo ='" + this.txtCodigo.Text.ToString() + "'"; }
            catch
            { }

            if (frmEntrada.dvUsers.Count > 0) this.txtNome.Text = frmEntrada.dvUsers[0].Row[1].ToString();
            else this.txtNome.Clear();

            if (string.IsNullOrEmpty(this.txtCodigo.Text)) this.txtNome.Clear();
        }

        private void lblVirtualkeyboard_Click(object sender, EventArgs e)
        {
            try
            { System.Diagnostics.Process.Start("OSK.exe"); }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void btCancelar_Click(object sender, EventArgs e)
        {
            this.FormClosing -= new FormClosingEventHandler(frmLogin_FormClosing);
            Application.Exit();
            this.txtCodigo.Focus();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            this.TopMost = true;
            Program.Entrada.LockTool.Image = Properties.Resources.LOCK;
            Program.Entrada.tmrLogin.Enabled = false;

            bool BtCancelar = true;

            foreach (Form Frm in Application.OpenForms)
            {
                if (Frm.Name != "frmEntrada" && Frm.Name != "frmLogin")
                    BtCancelar = false;
            }
            if (BtCancelar == true)
                this.btCancelar.Enabled = true;
            else
                this.btCancelar.Enabled = false;
        }

        private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }

        void LblVirtualkeyboardClick(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("OSK.exe");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtNome_TextChanged(object sender, EventArgs e)
        {

        }

        private void Label1_Click(object sender, EventArgs e)
        {

        }

        private void Label2_Click(object sender, EventArgs e)
        {

        }
    }
}
